package com.ddyx.util;

import java.io.File;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ddyx.mapper.BookingMapper;
import com.ddyx.mapper.GoodsMapper;
import com.ddyx.mapper.MessageFormMapper;
import com.ddyx.mapper.OrderMapper;

/**
 * @ClassName: CleanBookingUtil.java
 * @Description: 定时处理所有已过期数据 
 * @author: liuhoujie
 * @date: 2018年1月17日
 */
@Component
public class TimeHandlerUtil {

	@Resource
	BookingMapper bookingMapper;
	@Resource
	MessageFormMapper msgFormMapper;
	@Resource
	OrderMapper orderMapper;
	@Resource
	GoodsMapper goodsMapper;
	
	/**
	 * 定时处理 预约单
	 * 每1min
	 */
	@Lazy(true)
	@Scheduled(cron="0 0/1 * * * ?")
	public void cleanBooking() {
		bookingMapper.cleanBooking();
	}
	
	/**
	 * 定时处理 未支付订单 (未支付订单2小时自动取消)
	 * 每1min
	 */
	@Lazy(true)
	@Scheduled(cron="0 0/1 * * * ?")
	public void cancelUnpaidOrders() {
		orderMapper.cancelUnpaidOrders();
	}
	
	/**
	 * 定时处理 已支付订单 (已支付订单10天后自动完成)
	 * 每1min
	 */
//	@Lazy(true)
//	@Scheduled(cron="0 0/1 * * * ?")
//	public void confirmPaidOrders() {
//		
//	}
	
	/**
	 * 定时处理 商品下架(上架两周的商品 自动下架)
	 * 每1min
	 */
//	@Lazy(true)
//	@Scheduled(cron="0 0/1 * * * ?")
//	public void pullOffGoods() {
//		goodsMapper.pullOffGoods();
//	}
	
	/**
	 * 定时处理 formId过期
	 * 每10min
	 */
	@Lazy(true)
	@Scheduled(cron="0 0/10 * * * ?")
	public void cleanFormId() {
		msgFormMapper.cleanFormId();
	}
	
	/**
	 * 清除缓存定时任务
	 * 每天4-6点每隔半个小时执行一次 
	 */
	@Scheduled(cron="0 */30 4-6 * * ?")
	public void cleanCache() {
		String savePath = PropertiesUtil.getProperty("QRCode.logo");
		long nowtime = System.currentTimeMillis();

		File f = new File(savePath);
		if (!f.exists()) {
			System.out.println(savePath + " not exists");
		} else {
			File fa[] = f.listFiles();
			for (int i = 0; i < fa.length; i++) {
				File fs = fa[i];
				Long lasttime = fs.lastModified();
				// 返回文件最后修改时间，是以个long型毫秒数
				if (3600000 < (nowtime - lasttime)) {
					fs.delete();
				}
			}
		}
	}
	
	
}
